package com.bdqn.t350.util;


//为什么会出现现成安全问题？
//        多个线程抢一个资源
//
//        买票问题
public class Site implements Runnable {

    //共享资源 剩余票
    private int ticket = 10;
    //买到的是第几张票
    private int count = 0;
    //标识是否还有余票
    private boolean bool = true;

    private static Site site = null;

    static {
        System.out.println("代码块");
        System.out.println("new....");
        site = new Site();
    }

    private Site() {
        System.out.println("构造方法");
    }

    public static Site getInstance() {
        Site site = new Site();
        return site;
    }

    @Override
    public void run() {
        while (bool == true) {
            sale();
        }
    }

    public synchronized void sale() {
        if (ticket < 1) {
            bool = false;
        } else {
            ticket--;
            count++;
            try {
                System.out.println(Thread.currentThread().getName() + ",买到了第" + count + ",剩余" + ticket + "张票");
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


    public static void main(String[] args) {

    }
}
